<template>
  <div>
    <el-dialog :title="info.isAdd ? '活动添加' : '活动修改'" :visible.sync="info.isshow" @close="close">
      <el-form :model="user">
        <el-form-item label="活动名称" label-width="120px">
          <el-input v-model="user.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动期限" label-width="120px">
          <div class="demo-date-picker">
            <div class="block">
              <el-date-picker
                v-model="time"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="timestamp"
              />
            </div>
          </div>
        </el-form-item>
        <el-form-item label="一级分类" label-width="120px">
          <el-select v-model="user.first_cateid" placeholder="请选择分类" @change="change_first_id">
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" label-width="120px">
          <el-select v-model="user.second_cateid" placeholder="请选择分类" @change="change_second_id">
            <el-option
              v-for="item in second_cate_list"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品" label-width="120px">
          <el-select v-model="user.goodsid" placeholder="请选择商品">
            <el-option
              v-for="item in goods_list"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cal">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  reqseckadd,
  reqcatelist,
  reqgoodslist,
  reqseckinfo,
  reqseckedit
} from "../../../http/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { formClose } from "../../../mixins";

export default {
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      user: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1
      },
      second_cate_list: [],
      goods_list: [],
      time: ""
    };
  },
  computed: {
    ...mapGetters({
      cateList: "cate/arr"
    })
  },
  mounted() {
    this.getCateList();
  },
  methods: {
    ...mapActions({
      getCateList: "cate/reqArr",
      reqArr: "seckill/reqArr"
    }),
    checkdata() {
      return new Promise((resolve, reject) => {
        if (this.user.title == "") {
          errorAlert("活动名称不能为空");
          return;
        }
        if (this.time == "") {
          errorAlert("活动期限不能为空");
          return;
        }
        if (this.user.first_cateid == "") {
          errorAlert("一级分类不能为空");
          return;
        }
        if (this.user.second_cateid == "") {
          errorAlert("二级分类不能为空");
          return;
        }
        if (this.user.goodsid == "") {
          errorAlert("商品不能为空");
          return;
        }
        resolve();
      });
    },
    add() {
      this.checkdata().then(() => {
        reqseckadd({
          ...this.user,
          begintime: this.time[0],
          endtime: this.time[1]
        }).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            this.empty();
            this.info.isshow = false;
            this.reqArr();
          }
        });
      });
    },
    empty() {
      this.user = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1
      };
      this.second_cate_list = [];
      this.goods_list = [];
      this.time = [];
    },
    changepid() {
      if (this.user.pid === 0) {
        this.user.type = 1;
      } else {
        this.user.type = 2;
      }
    },
    getOne(id) {
      reqseckinfo({ id }).then(res => {
        if (res.data.code === 200) {
          this.user = res.data.list;
          this.time = [res.data.list.begintime, res.data.list.endtime];
          this.user.id = id;
        }
      });
    },
    update() {
      this.checkdata().then(() => {
        reqseckedit({
          ...this.user,
          begintime: this.time[0],
          endtime: this.time[1]
        }).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            this.empty();
            this.info.isshow = false;
            this.reqArr();
          }
        });
      });
    },
    get_second_cate_list() {
      reqcatelist({ pid: this.user.first_cateid }).then(res => {
        if (res.data.code === 200) {
          this.second_cate_list = res.data.list;
        }
      });
    },
    change_first_id() {
      this.user.second_cateid = "";
      this.get_second_cate_list();
    },
    get_goods_list() {
      reqgoodslist({
        fid: this.user.first_cateid,
        sid: this.user.second_cateid
      }).then(res => {
        if (res.data.code === 200) {
          this.goods_list = res.data.list;
        }
      });
    },
    change_second_id() {
      this.user.goodsid = "";
      this.get_goods_list();
    }
  }
};
</script>
<style scoped lang='less'>
@import "../../../less/index.less";
</style>